<template>
  <div class="app-container">
    <!-- 搜索和操作工具栏 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="list"
      border
      style="width: 100%"
    >
      <el-table-column label="标题" prop="title" />
      <el-table-column label="类型" prop="type">
        <template #default="{ row }">
          {{ getTypeName(row.type) }}
        </template>
      </el-table-column>
      <el-table-column label="申请人" prop="createBy" />
      <el-table-column label="申请时间" prop="createTime" />
      <el-table-column label="当前节点" prop="currentNode" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button type="text" @click="handleApprove(row)">审批</el-button>
          <el-button type="text" @click="handleView(row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="listQuery.page"
      v-model:limit="listQuery.limit"
      @pagination="getList"
    />

    <!-- 审批对话框 -->
    <el-dialog
      title="审批"
      v-model="approveDialogVisible"
      width="500px"
    >
      <el-form
        ref="approveForm"
        :model="approveForm"
        :rules="approveRules"
        label-width="80px"
      >
        <el-form-item label="审批意见" prop="comment">
          <el-input
            v-model="approveForm.comment"
            type="textarea"
            :rows="3"
            placeholder="请输入审批意见"
          />
        </el-form-item>
        <el-form-item label="审批结果" prop="result">
          <el-radio-group v-model="approveForm.result">
            <el-radio :label="true">同意</el-radio>
            <el-radio :label="false">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="approveDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitApprove">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getTodoTasks, approveTask } from '@/api/workflow/approval'
import Pagination from '@/components/Pagination'

export default {
  name: 'DocumentApproval',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: undefined
      },
      typeOptions: [
        { label: '通知公告', value: 1 },
        { label: '规章制度', value: 2 },
        { label: '会议纪要', value: 3 },
        { label: '其他', value: 4 }
      ],
      approveDialogVisible: false,
      approveForm: {
        taskId: undefined,
        comment: '',
        result: true
      },
      approveRules: {
        comment: [{ required: true, message: '请输入审批意见', trigger: 'blur' }],
        result: [{ required: true, message: '请选择审批结果', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getTodoTasks(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleView(row) {
      this.$router.push(`/document/view/${row.id}`)
    },
    handleApprove(row) {
      this.approveForm.taskId = row.taskId
      this.approveDialogVisible = true
    },
    submitApprove() {
      this.$refs['approveForm'].validate((valid) => {
        if (valid) {
          approveTask(this.approveForm).then(() => {
            this.$message({
              type: 'success',
              message: '审批成功'
            })
            this.approveDialogVisible = false
            this.getList()
          })
        }
      })
    },
    getTypeName(type) {
      const found = this.typeOptions.find(item => item.value === type)
      return found ? found.label : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding-bottom: 10px;
  .filter-item {
    margin-right: 10px;
  }
}
</style> 